// pages/login/adminUser/index.js
const api = require("../../../utils/interface.js")
let bok = true;
let bok2 = true
Page({
  data: {
    code: "",
    phone: "",
    text: "获取验证码",
    msdId: "",
    options:null,
    bok:true
  },
  onLoad: function(options) {
    console.log(options)
    if (options.state){
      this.data.options = JSON.parse(options.state)
    }
  },
  getPhone(e) {
    this.data.phone = e.detail.value
  },
  getCode() {
    if (!this.isPoneAvailable(this.data.phone)) {
      wx.showModal({
        title: '提示',
        content: "请输入正确的手机号",
        showCancel: false,
      })
      return false
    } else {
      if (!bok) return false;
      bok = false
      api.getVerificationCode({
        phone: this.data.phone
      }).then(res => {
        this.data.msdId = res.data.data
      })
      let n = 60;
      let timer = setInterval(() => {
        n--
        this.setData({
          text: "剩余" + n + "秒"
        })
        if (n <= 0) {
          clearInterval(timer), bok = true, n = 60, this.setData({
            text: "重新发送"
          })
        }
      }, 1000)
    }
  },
  isPoneAvailable($poneInput) {
    var myreg = /^[1][3,4,5,6,7,8,9][0-9]{9}$/;
    if (!myreg.test($poneInput)) {
      return false;
    } else {
      return true;
    }
  },
  isRealNum(val) {
    if (val === "" || val == null) {
      return false;　　
    }
    if (!isNaN(val)) {　　
      return true;　　
    } else {　　　　
      return false;　　
    }
  },

  formSubmit(e) {
    if (!bok2) {
      return false
    }
    bok2 = false
    if (!this.isPoneAvailable(e.detail.value.user)) {
      wx.showModal({
        title: '提示',
        content: "请输入正确的手机号",
        showCancel: false,
      })
      bok2 = true
      return false
    }
    if (!this.isRealNum(e.detail.value.pass)) {
      wx.showModal({
        title: '提示',
        content: "请输入正确的验证码",
        showCancel: false,
      })
      bok2 = true
      return false
    }
    var that = this;
    wx.login({
      success: function(res) {
        if (res.code) {
          that.data.code = res.code;
          api.getOpenId({
            code: that.data.code
          }).then(res => {
            wx.setStorageSync("openid", res.data.data)
            let data = {
              phone: e.detail.value.user,
              code: e.detail.value.pass,
              openid: res.data.data,
              msgId: that.data.msdId,
            }
            api.codeLogin(data).then(res => {
              bok2 = true
              if (res.data.success) {
                wx.setStorageSync('menberInfo', res.data.data)
                wx.setStorageSync('userPhone', res.data.data.phone)
                wx.setStorageSync("jwt", res.data.data.jwt)
                getApp().globalData.isLogin = true
                if (that.data.options){
                  if (that.data.options.form == "0") {
                    getApp().globalData.couponOptionsIndex = 99
                    wx.redirectTo({
                      url: '/pages/index/pages/nextY/nextY?scene=' + that.data.options.scene,
                    })
                  } else if (that.data.options.form == "1") {
                    getApp().globalData.couponOptionsIndex = 99
                    wx.redirectTo({
                      url: '/pages/public/pages/router/xiche?scene=' + that.data.options.scene,
                    })
                  } else if (that.data.options.form == "2") {
  
                    getApp().globalData.couponOptionsIndex = that.data.options.scene
                    console.log(getApp().globalData.couponOptionsIndex)

                    wx.switchTab({
                      url: '/pages/tabBar/index/index',
                    })
                  } else if (that.data.options.form == "3") {
                    getApp().globalData.couponOptionsIndex = 99
                    wx.redirectTo({
                      url: '/pages/index/pages/counp/counp',
                    })
                  }
                  else if (that.data.options.form == "4") {
                    getApp().globalData.couponOptionsIndex = 99
                    wx.redirectTo({
                      url: '/pages/public/pages/jianhanghuodong/resetETC?scene=' + JSON.stringify(that.data.state),
                    })
                  } else if (that.data.options.form == "5") {
                    getApp().globalData.couponOptionsIndex = 99
                    console.log(that.data)
                    wx.redirectTo({
                      url: '/pages/public/pages/counpInfo/index?data=' + JSON.stringify(that.data.options.data),
                    })
                  }
                  else if (that.data.options.form == "6") {
                    getApp().globalData.couponOptionsIndex = 99
                    wx.redirectTo({
                      url: '/pages/person/pages/shareaBox/shareaBox?name=gzh',
                    })
                  } 
                  
                   else {
                    wx.switchTab({
                      url: '/pages/tabBar/index/index',
                    })
                  }
                } else {
                  wx.switchTab({
                    url: '/pages/tabBar/index/index',
                  })
                }
              } else {
                wx.showModal({
                  title: '提示',
                  content: res.data.msg,
                })
              }
            })
          })
        } else {
          wx.showModal({
            title: '提示',
            content: '登录失败，未获取到openId',
            showCancel: false,
            success: function () {
              wx.switchTab({
                url: '/pages/tabBar/index/index',
              })
            }
          })
        }
      },
      fail: function() {
        wx.showModal({
          title: '提示',
          content: '登录失败，未获取到openId',
          showCancel: false,
          success: function () {
            wx.switchTab({
              url: '/pages/tabBar/index/index',
            })
          }
        })
      },
    });
    
  }
})